<!--
 * @Author: your name
 * @Date: 2020-11-10 15:08:44
 * @LastEditTime: 2020-11-19 09:46:34
 * @LastEditors: Please set LastEditors
 * @Description: 搜索条件封装
 * @FilePath: \vue-admin-beautiful-pro-template\src\components\HH-search.vue
-->
<template>
  <div class="search-container">
    <el-form :model="filterObject" inline>
      <el-row :gutter="10">
        <el-col
          v-for="item in searchOptions"
          :key="item.label"
          :span="item.span || 6"
        >
          <el-form-item :label="item.label" :label-width="item.width">
            <el-select
              v-if="item.type === 'select'"
              v-model="filterObject[item.prop]"
              clearable
              :filterable="item.filterable"
              :placeholder="item.placeholder"
              @change="handleChange"
            >
              <template v-if="item.nameValue">
                <el-option
                  v-for="sub in item.arr"
                  :key="sub[item.nameValue[1]]"
                  :label="sub[item.nameValue[0]]"
                  :value="sub[item.nameValue[1]]"
                ></el-option>
              </template>
              <template v-else>
                <el-option
                  v-for="sub in item.arr"
                  :key="sub.value"
                  :label="sub.label"
                  :value="sub.value"
                ></el-option>
              </template>
            </el-select>

            <el-date-picker
              v-if="item.type === 'date'"
              v-model="filterObject[item.prop]"
              format="yyyy 年 MM 月 dd 日"
              :placeholder="item.placeholder"
              :value-format="item.valueFormat"
              @change="handleChange"
            ></el-date-picker>

            <el-date-picker
              v-if="item.type === 'daterange'"
              v-model="filterObject[item.prop]"
              type="daterange"
              format="yyyy 年 MM 月 dd 日"
              :start-placeholder="item.startPlaceholder"
              :end-placeholder="item.endPlaceholder"
              :value-format="item.valueFormat"
              @change="handleChange"
            ></el-date-picker>

            <el-input
              v-if="item.type === 'text'"
              v-model="filterObject[item.prop]"
              clearable
              size="small"
              :placeholder="item.placeholder"
              prefix-icon="el-icon-search"
            ></el-input>

            <!-- 区间输入框 -->
            <div v-if="item.type === 'doubleText'">
              <el-input
                v-model="filterObject[item.prop[0]]"
                :placeholder="item.placeholder[0]"
                style="width: 130px"
                clearable
              ></el-input>
              <span class="line">~</span>
              <el-input
                v-model="filterObject[item.prop[1]]"
                :placeholder="item.placeholder[1]"
                style="width: 130px"
                clearable
              ></el-input>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col
          v-if="btnOption == 1"
          style="display: flex; justify-content: flex-end"
        >
          <el-button type="primary" size="small" @click="hangleSearch">
            搜索
          </el-button>
          <el-button type="primary" size="small" @click="hangleReset">
            重置
          </el-button>
        </el-col>
        <el-col
          v-if="btnOption == 2"
          style="display: flex; justify-content: flex-end"
        >
          <el-button type="primary" size="small" @click="hangleSearch">
            搜索
          </el-button>
          <el-button type="primary" size="small" @click="hangleReset">
            重置
          </el-button>
          <el-button type="primary" size="small" @click="hangleExport">
            导出
          </el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
  export default {
    props: {
      // 搜索项
      searchOptions: {
        type: Array,
        default() {
          return []
        },
        require: true,
      },
      btnOption: {
        type: Number,
        default: 1,
      },
    },
    emits: ['search', 'reset', 'export'],
    data() {
      return {
        filterObject: {},
      }
    },
    methods: {
      //  清空搜索项
      removeFilterArray() {
        this.filterObject = {}
      },
      handleChange() {},
      hangleSearch() {
        this.$emit('search', this.filterObject)
      },
      hangleReset() {
        this.$emit('reset', this.filterObject)
      },
      hangleExport() {
        this.$emit('export', this.filterObject)
      },
    },
  }
</script>

<style lang="scss" scope>
  @import '../assets/styles/common.css';
  .line {
    display: inline-block;
    width: 30px;
    height: 30px;
    line-height: 30px;
    text-align: center;
  }
</style>
